// Package controller
// Author liuzhiyong
// Date 2023/11/15
// Description 注册路由
package controller

import (
	"github.com/gin-gonic/gin"
	"shopping/config"
	"shopping/controller/category"
	"shopping/controller/product"
	"shopping/controller/user"
	"shopping/dao/mysql"
	"shopping/service"
	"shopping/utils/middleware"
)

// RegisterHandlers
// Description: 注册所有控制器
// param: r gin引擎
func RegisterHandlers(r *gin.Engine) {
	daos := createDaos()
	registerCategoryHandlers(r, daos)
	registerUserHandlers(r, daos)
	//registerCartHandlers(r, daos)
	registerProductHandlers(r, daos)
	registerOrderHandlers(r, daos)
}

// Daos
// Description: 所有Dao
type daos struct {
	categoryDao    *mysql.CategoryDao
	userDao        *mysql.UserDao
	productDao     *mysql.ProductDao
	cartDao        *mysql.CartDao
	cartItemDao    *mysql.ItemDao
	orderDao       *mysql.OrderDao
	orderedItemDao *mysql.OrderItemDao
}

// CreateDaos
// Description: 注如数据库连接
// return: *daos
func createDaos() *daos {
	return &daos{
		categoryDao:    mysql.NewCategoryDao(mysql.Db),
		userDao:        mysql.NewUserDao(mysql.Db),
		productDao:     mysql.NewProductDao(mysql.Db),
		cartDao:        mysql.NewCartDao(mysql.Db),
		cartItemDao:    mysql.NewItemDao(mysql.Db),
		orderDao:       mysql.NewOrderDao(mysql.Db),
		orderedItemDao: mysql.NewOrderItemDao(mysql.Db),
	}
}

// registerCategoryHandlers
// Description: 注册分类控制器
// param r gin引擎
// param daos 所有持久层
func registerCategoryHandlers(r *gin.Engine, daos *daos) {
	// 创建实例, 注入依赖
	categoryService := service.NewCategoryService(daos.categoryDao)
	categoryController := category.NewCategoryController(categoryService)
	// 添加组路由
	categoryGroup := r.Group("/category")
	// 注册接口
	//categoryGroup.POST("", middleware.AuthAdminMiddleware(config.Conf.JwtConfig.SecretKey), categoryController.CreateCategory)
	categoryGroup.POST("", categoryController.CreateCategory)
	categoryGroup.GET("", categoryController.GetCategories)
	categoryGroup.POST("/upload", middleware.AuthAdminMiddleware(config.Conf.JwtConfig.SecretKey), categoryController.BulkCreateCategory)
}

// registerUserHandlers
// Description: 注册用户控制器
// param r gin引擎
// param daos 所有持久层
func registerUserHandlers(r *gin.Engine, daos *daos) {
	// 创建实例注册依赖
	userService := service.NewUserService(daos.userDao)
	userController := user.NewUserController(userService)

	//添加路由分组
	userGroup := r.Group("/user")
	// 注册接口路由
	userGroup.POST("", userController.CreateUser)
	userGroup.POST("/login", userController.Login)
}

// registerCartHandlers
// Description: 注册购物车控制器
// param r gin引擎
// param daos 所有持久层
func registerCartHandlers(r *gin.Engine, daos *daos) {
	service.NewCartService(daos.cartDao, daos.cartItemDao, daos.productDao)
}

// registerProductHandlers
// Description: 注册购商品控制器
// param r gin引擎
// param daos 所有持久层
func registerProductHandlers(r *gin.Engine, daos *daos) {
	productService := service.NewProductService(daos.productDao)
	productController := product.NewProductController(productService)

	// 添加分组路由, 分组添加中间件
	productGroup := r.Group("/cart", middleware.AuthAdminMiddleware(config.Conf.JwtConfig.SecretKey))
	productGroup.GET("", productController.GetProducts)
	productGroup.POST("", productController.CreateProduct)
	productGroup.DELETE("", productController.DeleteProduct)
	productGroup.PATCH("", productController.UpdateProduct)
}

// registerOrderHandlers
// Description: 注册购订单控制器
// param r gin引擎
// param daos 所有持久层
func registerOrderHandlers(r *gin.Engine, daos *daos) {

}
